//
//  RYYuViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/12.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class RYYuViewController: SYBaseViewController {
    
    // MARK: - properties
    var id: Int!
    
    
    
    var headerView: UIView = UIView()
    
    fileprivate var topView: RYYuTopView = RYYuTopView()
    
    fileprivate var middleView: RYYuMiddleView = RYYuMiddleView()
    
    fileprivate var bottomView: RYYBuyToolView = RYYBuyToolView()
    
    fileprivate var firvc: RYYuDetailViewController!
     
    fileprivate var secvc: RYYuShengzhangViewController!
    
    fileprivate var thirvc: RYYuSuyuanViewController!

    var pageView: ENestScrollPageView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    init(id: Int) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        firvc = RYYuDetailViewController(id: self.id)
        firvc.loboVC = self
        firvc.view.backgroundColor = .white
        
        secvc = RYYuShengzhangViewController(id: self.id)
        secvc.loboVC = self
        secvc.view.backgroundColor = .white
        
        thirvc = RYYuSuyuanViewController(id: self.id)
        thirvc.loboVC = self
        thirvc.view.backgroundColor = .white
        
        
        setupTopView()
        setupBottomView()
        reloadLists()
//        NotificationCenter.default.addObserver(self, selector: #selector(refreshPunchVC), name: .Noti_SkipToLoginView, object: nil)
    }
    
    
    // MARK: - public methods
    public func refreshItemsNum() {
        
    }
    
    // MARK: - event response
    @objc func refreshPunchVC() {

    }
    
    // MARK: - private methods
    fileprivate func topMangain() {
        // 解决Webview未覆盖顶部状态栏
        if #available(iOS 11.0, *) {
            pageView.eScroll().contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        } else {
            self.edgesForExtendedLayout = UIRectEdge.bottom
        }
    }
    
    func creatPageView(_ head:UIView) {
        let p1: EScrollPageItemBaseView = firvc.bgView
        let p2: EScrollPageItemBaseView = secvc.bgView
        let p3: EScrollPageItemBaseView = thirvc.bgView
        let param = ENestParam()
        param.scrollParam.segmentParam.lineColor = UIColor(hex: 0x367FF6)
        param.scrollParam.segmentParam.textSelectedColor = 0x367FF6
        param.scrollParam.segmentParam.textColor = 0x1D1D1D
        param.scrollParam.segmentParam.fontSize = 15.autoWidth()
        param.scrollParam.headerHeight = 40.autoWidth()
        
        param.yOffset = NAV_HEIGHT
        pageView = ENestScrollPageView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - BOTTOM_SAFE_HEIGHT), head: head, subDataViews: [p1, p2, p3], setParam: param)
        self.view.addSubview(pageView)
        self.view.addSubview(bottomView)
        topMangain()
    }
            
    fileprivate func setupTopView() {
        topView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 533.autoWidth() + STATUSBAR_HEIGHT)
        topView.currentVC = self
        middleView.frame = CGRect(x: 0, y: 533.autoWidth() + STATUSBAR_HEIGHT, width: SCREEN_WIDTH, height: 214.autoWidth())
        
        headerView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 747.autoWidth() + STATUSBAR_HEIGHT)
        headerView.addSubview(topView)
        headerView.addSubview(middleView)
        
    }
    
    fileprivate func setupBottomView() {
        bottomView.frame = CGRect(x: 0, y: SCREEN_HEIGHT - BOTTOM_SAFE_HEIGHT - 49.autoWidth(), width: SCREEN_WIDTH, height: 49.autoWidth())
        bottomView.currentVC = self
        bottomView.renyang.addAction {
            let vc = RYTijiaoViewController(params: self.secvc.model)
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
    func loadDataSuccessed() {
        
    }
    
    func getDetailData(id : Int, success:@escaping (RYProductDetailModel)->()) {
        requestVM.baseRequest(disposeBag: dispose, type: .nongchangProductDetail(id: id), modelClass: SGBaseModel<RYProductDetailModel>.self) {[weak self] (res) in
            if let `self` = self {
                let model:RYProductDetailModel = res.data ?? RYProductDetailModel()
                self.topView.configureView(model)
                self.middleView.configureView(model)
                DispatchQueue.main.async {
                    self.creatPageView(self.headerView)
                }
                let urls:[String] = model.detailImgUrl?.components(separatedBy: ",") ?? []
                var arr:[String] = []
                for (_,value) in urls.enumerated() {
                    arr.append("\(IP)\(value)")
                }
                self.firvc.imageUrls = arr
                self.secvc.model = model
                self.thirvc.model = model
                
                success(model)
            }
            
        } Error: {
            
        }
        
    }
    fileprivate func reloadLists() {
        // 不是依次执行，只是全部完成后给通知
//        HUDUtil.showBlackIndiView()
        let queue = DispatchQueue(label: "getlist")
        let group  = DispatchGroup()
        queue.async(group: group, execute: {
            group.enter()

            self.getDetailData(id: self.id) {[weak self] model in
                if let `self` = self {
                                        DispatchQueue.main.async {
//                                            weakSelf.refreshItemsNum()
                                        }
                                        group.leave()
                }
            }
        })
 
        group.notify(queue: queue) {
            DispatchQueue.main.async {
                HUDUtil.hideHud()
                self.loadDataSuccessed()
            }
        }
    }
        
 
}



